limsMaterialApply.editDetailPage = function (step) {

    return {
        getBtnToolbar: function () {
            var _this = this
            return {
                type: 'btnToolbar',
                items: [{
                    type: 'button',
                    text: 'GIKAM.BUTTON.BACK',
                    icon: 'back',
                    onClick: function () {
                        workspace.window.goBack()
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                    icon: 'submit', color: 'success',
                    hidden: step !== 'edit',
                    onClick: function () {
                        _this.submitSingle()
                    }
                }, {
                    type: 'button',
                    text: step === 'audit' ? 'GIKAM.WORKFLOW.BUTTON.PASS' : 'T_LIMS_MATERIAL_APPLY.OUT_SUCCESS',
                    icon: 'pass',
                    hidden: step === 'edit',
                    onClick: function () {
                        _this.passSingle()
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                    icon: 'reject',
                    hidden: step !== 'audit',
                    onClick: function () {
                        _this.rejectSingle()
                    }
                }]
            }
        },

        submitSingle: function () {
            let _this = this
            Gikam.create('workflow').submit({
                data: [{id: _this.param.id}],
                pageObject: limsMaterialApply,
                formatterToolbar: (toolbar, type, formId) => {
                    toolbar.forEach(b => {
                        if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                            b.class = 'blue'
                            b.icon = 'save'
                            b.color = 'white'
                        } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                            b.icon = 'back'
                        }
                    })
                    return toolbar
                }
            }).done(function () {
                workspace.window.goBack()
            })
        },

        passSingle: function () {
            let _this = this
            Gikam.create('workflow').pass({
                data: [{id: _this.param.id}],
                pageObject: limsMaterialApply,
                formatterToolbar: (toolbar, type, formId) => {
                    toolbar.forEach(b => {
                        if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                            b.class = 'blue'
                            b.icon = 'save'
                            b.color = 'white'
                        } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                            b.icon = 'back'
                        }
                    })
                    return toolbar
                }
            }).done(function () {
                workspace.window.goBack()
            })
        },

        rejectSingle: function () {
            let _this = this
            Gikam.create('workflow').reject({
                data: [{id: _this.param.id}],
                pageObject: limsMaterialApply,
                formatterToolbar: (toolbar, type, formId) => {
                    toolbar.forEach(b => {
                        if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                            b.class = 'blue'
                            b.icon = 'save'
                            b.color = 'white'
                        } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                            b.icon = 'back'
                        }
                    })
                    return toolbar
                }
            }).done(function () {
                workspace.window.goBack()
            })
        },

        getMaterialOutGrids: function (applyId) {
            let _this = this;
            let columns = [];
            if (step === 'edit') {
                columns = limsMaterialApply.getMaterialOutEditListColumns();
            } else {
                columns = limsMaterialApply.getMaterialOutAuditListColumns(step);
            }
            return {
                type: 'tab',
                panels: [{
                    title: 'T_LIMS_MATERIAL_OUT.SERVICE_NAME',
                    items: [{
                        type: 'grid',
                        id: _this.idManager.materialOutGrid,
                        service: 'limsMaterialOutServiceImpl',
                        dbTable: 'T_LIMS_MATERIAL_OUT',
                        columns: columns,
                        onLoadSuccess: function (data) {
                            if (!Gikam.isEmpty(data)) {
                                this.activeRowByIndex(0)
                            } else {
                            }
                        },
                        onRowActive: function (index, row) {
                        },
                        deleteFormatter: function (row) {
                            return row.matCode || row.id
                        },
                        url: _this.urlManager.materialOutGrid + '/queries',
                        requestData: {
                            applyId_EQ: applyId
                        },
                        toolbar: [{
                            type: 'button',
                            text: 'GIKAM.BUTTON.INSERT',
                            icon: 'add',
                            hidden: step !== 'edit',
                            onClick: function () {
                                var applyId = _this.param.id
                                var batchNo = _this.param.applycode
                                Gikam.create('modal', {//材料信息查询
                                    title: Gikam.choose.getConfig("material").title,
                                    id: 'lsi-outStandard-list-grid',
                                    url: IFM_CONTEXT + Gikam.choose.getConfig("material").url + Gikam.param({
                                        target_filter: encodeURI(JSON.stringify({
                                            type: 'unassigned',
                                            targetTable: 'T_LIMS_MATERIAL_OUT',
                                            targetMatchColumn: 'MATCODE',
                                            thisMatchColumn: 'MATCODE',
                                            filter: [{
                                                targetFilterColumn: 'APPLYID',
                                                targetFilterValue: applyId
                                            }]
                                        })),
                                        status_EQ: '1'
                                    }),
                                    onAfterClose: function (rows) {
                                        if (Gikam.isEmpty(rows)) {
                                            return
                                        }
                                        workspace.window.showMask()
                                        Gikam.postText(_this.urlManager.materialOutGrid, Gikam.getJsonWrapper({
                                            applyId: applyId,
                                            batchNo: batchNo
                                        }, ['', rows])).done(function (result) {
                                            Gikam.getComp(_this.idManager.materialOutGrid).refresh()
                                        }).always(function () {
                                            workspace.window.closeMask()
                                        })
                                    }
                                })
                            }
                        }, {
                            type: 'button',
                            text: 'GIKAM.BUTTON.DELETE',
                            icon: 'remove-row',
                            hidden: step !== 'edit',
                            onClick: function () {
                                Gikam.getComp(_this.idManager.materialOutGrid).deleteRows(_this.urlManager.materialOutGrid)
                            }
                        }]
                    }]
                }, {
                    title: 'GIKAM.FILE.ATTACHMENT',
                    items: [{
                        type: 'uploader',
                        id: _this.idManager.materialOutGrid + '-uploader',
                        filter: false,
                        dbTable: 'T_LIMS_MATERIAL_APPLY',
                        bizId: applyId,
                        readonly: !step || step !== 'edit'
                    }]
                },{ //审核记录
                    title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                    items: [{
                        type: 'grid',
                        filter: false,
                        page: false,
                        toolbarHidden: true,
                        url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_MATERIAL_APPLY&id=' + applyId,
                        columns: limsMaterialApply.getFlowListColumns()
                    }]
                }]
            }
        },

        idManager: {
            materialApplyGrid: step === 'edit' ? 'lims-material-apply-edit-list-grid' : ('lims-material-apply-audit-list-grid-' + step),
            materialOutGrid: 'lims-material-out-edit-list-grid-' + step,
            flowGridId: 'lims-material-out-operation-record-' + step,
        },

        urlManager: {
            materialApplyGrid: limsMaterialApply.baseUrl,
            materialOutGrid: IFM_CONTEXT + '/secure/basemodule/rm/material-outs'
        },

        submit: function () {
            var _this = this;
            // var grid = Gikam.getComp(_this.idManager.materialApplyGrid)
            Gikam.create('workflow').submit({
                data: [{id: _this.param.id}],
                pageObject: limsMaterialApply,
                formatterToolbar: (toolbar, type, formId) => {
                    toolbar.forEach(b => {
                        if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                            b.class = 'blue'
                            b.icon = 'save'
                            b.color = 'white'
                        } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                            b.icon = 'back'
                        }
                    })
                    return toolbar
                }
            }).done(function () {
                workspace.window.goBack()
            })
        },

        createPage: function () {
            let _this = this
            Gikam.create('layout', {
                center: {
                    items: [this.getBtnToolbar(), this.getMaterialOutGrids(_this.param.id)]
                },
                toolbarAlign: 'right',
                renderTo: workspace.window.$dom
            })
            return this
        },

        init: function (param) {
            this.param = param
            this.createPage()
        }
    }
}
